package com.tj.bigdata.syncblock;

import com.tj.bigdata.syncblock.strategy.SyncBlockStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableAsync;
import tk.mybatis.spring.annotation.MapperScan;

import java.io.IOException;

/**
 * @author guoch
 */
@Slf4j
@EnableAsync
@MapperScan(basePackages = {"com.tj.bigdata.syncblock.mapper"})
@SpringBootApplication
public class SyncBlockApplication implements CommandLineRunner {


    private SyncBlockStrategy syncBlockStrategy;

    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Autowired
    public void setSyncBlockStrategy(@Qualifier("streamSourceAndEsImpl") SyncBlockStrategy syncBlockStrategy) {
        this.syncBlockStrategy = syncBlockStrategy;
    }

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(SyncBlockApplication.class);
        application.addListeners(new ApplicationPidFileWriter("/var/log/syncBlock/app.pid"));
        application.run();
    }

    @Override
    public void run(String... args) throws IOException {

        log.info("大数据 - 区块同步 - 开始");
        syncBlockStrategy.batchBlockchain();
    }
}
